Paul's JavaScript Examples    
 
Check an entered E-Mail address for obvious errors

This script will check an entered E-mail address for obvious errors such as a missing @, spaces and other such simple things. Quite useful to help 'newbies' correctly fill in their email address and forego such classics as gte.net!"user@gte.net" that will undoubtedly bounce.

Example

Please enter your email address:

Usage

<INPUT TYPE="TEXT" NAME="Email" VALUE="" SIZE="30" MAXLENGTH="60" onChange="Check_Email(this.value)">

Source

<SCRIPT LANGUAGE="javascript">
<!--
function Check_Email(item)
{
        var returnVal = false
        checkVal = 0
        count1 = 0
        count2 = 0
        for (var i=0; i < item.length; i++)
        {
                if (item.substring(i,i+1) == '@')
                {
                        checkVal = checkVal + 1
                        count1 = count1 + 1
                }
                if (item.substring(i,i+1) == '!')
                {
                        checkVal = checkVal + 2
                        count2 = count2 + 1
                }
                if (item.substring(i,i+1) == '.')
                {
                        checkVal = checkVal + 4
                }
                if (item.substring(i,i+1) == ' ')
                {
                        checkVal = 8
                }
        }
        if (checkVal == 5 || checkVal == 6)
        {
                returnVal = true
        }
        else

        if (checkVal == 0) fout = 'Address not in user@domain.suffix format'
        if (checkVal == 1) fout = 'Didn\'t find any \'.\' in domain part of e-mail address'
        if (checkVal == 2) fout = 'Didn\'t find any \'.\' in domain part of e-mail address.'
        if (checkVal == 3) fout = 'Found both a \'@\' and a \'!\' seperator.\nThat is not allowed!'
        if (checkVal == 4) fout = 'Didn\'t find a \'@\' or \'!\' seperator.\nThat is not allowed!'
        if (checkVal == 7) fout = 'Found both a \'@\' and a \'!\' seperator.\nThat is not allowed!'
        if (checkVal > 7)  fout = 'Found space(s) in the E-mail address.\nThat is not allowed!'

        if (count1 > 1) fout =    'Found multiple instances of \'@\' separator.\nOnly one instance allowed!'
        if (count2 > 1) fout =    'Found multiple instances of \'!\' separator.\nOnly one instance allowed!'
        
        if (returnVal == false)
        {
                alert('Your email address seems incorrect.\nSo please doublecheck your input!\n\n \nError Report:\n' + fout)
        }
        return returnVal
}
// -->
</SCRIPT>